<?php

declare(strict_types=1);

namespace app\shop;

use app\shop\library\activity\Activity;
use app\shop\library\activity\ActivityRedis;
use app\shop\model\activity\Activity as ActivityModel;
use think\facade\Event;

/**
 * Shop 模块注册
 */
class Shop
{
    // 应用全局注册
    public function boot()
    {
        $this->appRegister();
        $this->eventRegister();
    }

    // 注册活动相关服务
    protected function appRegister()
    {
        app()->bind('activity', function () {
            return new Activity(ActivityModel::class);
        });

        app()->bind('activity_redis', function () {
            return new ActivityRedis();
        });
        $config = config('wechat')['defaults'];

        app()->bind('shop.wechat.official_account', function () use ($config) {
            $officialAccount = sheep_config('shop.platform.WechatOfficialAccount');
            $config = array_merge($config, [
                // AppID
                'app_id'  => $officialAccount['app_id'],
                // AppSecret
                'secret'  => $officialAccount['secret'],
            ]);
            $app = new \EasyWeChat\OfficialAccount\Application($config);
            if ($config['use_think_cache']) {
                $app['cache'] = cache();
            }
            return $app;
        });
        app()->bind('shop.wechat.mini_program', function () use ($config) {
            $miniProgram = sheep_config('shop.platform.WechatMiniProgram');
            $config = array_merge($config, [
                // AppID
                'app_id'  => $miniProgram['app_id'],
                // AppSecret
                'secret'  => $miniProgram['secret'],
            ]);
            $app = new \EasyWeChat\MiniProgram\Application($config);
            if ($config['use_think_cache']) {
                $app['cache'] = cache();
            }
            return $app;
        });
        app()->bind('shop.wechat.open_platform', function () use ($config) {
            $openPlatform = sheep_config('shop.platform.App');
            $config = array_merge($config, [
                // AppID
                'app_id'  => $openPlatform['app_id'],
                // AppSecret
                'secret'  => $openPlatform['secret'],
            ]);
            $app = new \EasyWeChat\OpenPlatform\Application($config);
            if ($config['use_think_cache']) {
                $app['cache'] = cache();
            }
            return $app;
        });
    }

    // 注册商城相关事件监听
    protected function eventRegister()
    {
        // 用户事件订阅
        Event::subscribe('app\shop\subscribe\User');
        // 商品事件订阅
        Event::subscribe('app\shop\subscribe\Goods');
        // 订单事件订阅
        Event::subscribe('app\shop\subscribe\Order');
        // 活动事件订阅
        Event::subscribe('app\shop\subscribe\Activity');
        // 售后事件订阅
        Event::subscribe('app\shop\subscribe\OrderAftersale');
        // -- commission code start --
        // 分销事件订阅
        Event::subscribe('app\shop\subscribe\Commission');
        // -- commission code end --
    }
}
